package com.whmskj.xjlhbc.mobile.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.db.DataSourceType;
import com.whmskj.xjlhbc.db.DynamicDataSourceContextHolder;
import com.whmskj.xjlhbc.domain.*;
import com.whmskj.xjlhbc.dto.JzkDto;
import com.whmskj.xjlhbc.mapper.*;
import com.whmskj.xjlhbc.mobile.GetStartDate;
import com.whmskj.xjlhbc.service.IMesjzkService;
import com.whmskj.xjlhbc.service.IStopdataInfoClassService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 设备
 */
@Tag(name = "设备", description = "设备")
@RestController
public class MachineryController {

    @Autowired
    private IMesjzkService service;

    @Autowired
    private MeskyjMapper meskyjMapper;

    @Autowired
    private MeskyjHistoryMapper meskyjHistoryMapper;

    @Autowired
    private MeskyjScMapper meskyjScMapper;

    @Autowired
    private MeskyjScHistoryMapper meskyjScHistoryMapper;

    @Autowired
    private MesglMapper mesglMapper;

    @Autowired
    private ScglMapper scglMapper;

    @Autowired
    private MesktMapper mesktMapper;

    @Autowired
    private MesktHistoryMapper mesktHistoryMapper;

    @Autowired
    private MesdbRealMapper mesdbRealMapper;

    @Autowired
    private MesdbClassMapper mesdbClassMapper;

    @Autowired
    private MesdbDayMapper mesdbDayMapper;

    @Autowired
    private MesdbHistoryMapper mesdbHistoryMapper;

    @Autowired
    private Bcxt06DistributionRoomElectricitMapper electricitMapper;

    @Autowired
    private IStopdataInfoClassService stopdataInfoClassService;

    @GetMapping("/jzk/tj")
    @Operation(description = "经轴库-统计")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, JzkDto.class})
    ))
    public AjaxResult tj() {
        JzkDto result = service.jzkTj();
        return AjaxResult.success(result);
    }

    @GetMapping("/jzk/list")
    @Operation(description = "经轴库-列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Mesjzk.class})
    ))
    public AjaxResult list() {
        List<List<Mesjzk>> list = service.queryList();
        return Page.getDataTable(list);
    }

    @GetMapping("/kyj/list")
    @Operation(description = "空压机-列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, MesKyj.class})
    ))
    public AjaxResult kyjList() {
        LambdaQueryWrapper<MesKyj> wrapper = new LambdaQueryWrapper<>();
        List<MesKyj> list = meskyjMapper.selectList(wrapper);
        list.forEach(item -> {
            String cs = "2111020002".equals(item.getMid()) || "2111020153".equals(item.getMid()) ? "鲍斯" : "虎渡";
            item.setCs(cs);
        });
        return AjaxResult.success(list);
    }

    @GetMapping("/kyj/historyList")
    @Operation(description = "空压机-历史记录",parameters = {
            @Parameter(name = "mid",description = "编号",required = true),
            @Parameter(name = "date",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, MesKyjHistory.class})
    ))
    public AjaxResult kyjHistoryList(String mid, @RequestParam(required = false) String date) {
        Page.startPage();
        DateTime dt = StringUtils.hasText(date) ? DateUtil.parse(date) : DateUtil.date();
        DateTime beginOfDay = DateUtil.beginOfDay(dt);
        DateTime endOfDay = DateUtil.endOfDay(dt);
        LambdaQueryWrapper<MesKyjHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesKyjHistory::getMid, mid).between(MesKyjHistory::getCreateDate, beginOfDay.getTime(), endOfDay.getTime())
                .orderByDesc(MesKyjHistory::getCreateDate);
        List<MesKyjHistory> list = meskyjHistoryMapper.selectList(wrapper);
        list.forEach(item -> {
            String cs = "2111020002".equals(item.getMid()) || "2111020153".equals(item.getMid()) ? "鲍斯" : "虎渡";
            item.setCs(cs);
        });
        return Page.getDataTable(list);
    }

    @GetMapping("/sc/kyj/list")
    @Operation(description = "空压机-列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, MesKyj.class})
    ))
    public AjaxResult sckyjList() {
        LambdaQueryWrapper<MesKyjSc> wrapper = new LambdaQueryWrapper<>();
        List<MesKyjSc> list = meskyjScMapper.selectList(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/sc/kyj/historyList")
    @Operation(description = "空压机-历史记录",parameters = {
            @Parameter(name = "mid",description = "编号",required = true),
            @Parameter(name = "date",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, MesKyjHistory.class})
    ))
    public AjaxResult sckyjHistoryList(String mid, @RequestParam(required = false) String date) {
        Page.startPage();
        DateTime dt = StringUtils.hasText(date) ? DateUtil.parse(date) : DateUtil.date();
        DateTime beginOfDay = DateUtil.beginOfDay(dt);
        DateTime endOfDay = DateUtil.endOfDay(dt);
        LambdaQueryWrapper<MesKyjScHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesKyjScHistory::getMid, mid).between(MesKyjScHistory::getCreateDate, beginOfDay.getTime(), endOfDay.getTime())
                .orderByDesc(MesKyjScHistory::getCreateDate);
        List<MesKyjScHistory> list = meskyjScHistoryMapper.selectList(wrapper);
        return Page.getDataTable(list);
    }

    @GetMapping("/gl/list")
    @Operation(description = "锅炉-列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Mesgl.class})
    ))
    public AjaxResult glList() {
        LambdaQueryWrapper<Mesgl> wrapper = new LambdaQueryWrapper<>();
        List<Mesgl> list = mesglMapper.selectList(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/gl/historyList")
    @Operation(description = "锅炉-历史记录",parameters = {
            @Parameter(name = "macid",description = "编号",required = true),
            @Parameter(name = "date",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Mesgl.class})
    ))
    public AjaxResult glHistoryList(String macid, @RequestParam(required = false) String date) {
        Page.startPage();
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SC.name());
        LambdaQueryWrapper<Scgl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Scgl::getDevice, macid)
                .like(StringUtils.hasText(date), Scgl::getTs, date)
                .orderByDesc(Scgl::getId);
        List<Scgl> list = scglMapper.selectList(wrapper);
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        return Page.getDataTable(list);
    }

    @GetMapping("/kt")
    @Operation(description = "空调")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Meskt.class})
    ))
    public AjaxResult kt() {
        LambdaQueryWrapper<Meskt> wrapper = new LambdaQueryWrapper<>();
        List<Meskt> list = mesktMapper.selectList(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/kt/history")
    @Operation(description = "空调历史数据",parameters = {
            @Parameter(name = "macid",description = "编号",required = true),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult ktHistory(String macid) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> topMap = new HashMap<>();
        Map<String, Object> chartMap = new HashMap<>();
        DateTime monthStartDate = DateUtil.parse(GetStartDate.getMonthStartDate());
        DateTime classStartDate = DateUtil.parse(GetStartDate.getClassStartDate());
        DateTime now = DateUtil.date();
        DateTime startDate = DateUtil.beginOfDay(now);

        LambdaQueryWrapper<Meskt> mesktLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mesktLambdaQueryWrapper.eq(BaseEntity::getMacid, macid);
        Meskt meskt = mesktMapper.selectOne(mesktLambdaQueryWrapper);
        LambdaQueryWrapper<MesktHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseEntity::getMacid, macid);
        wrapper.ge(BaseEntity::getTs, monthStartDate);
        List<MesktHistory> mesktHistories = mesktHistoryMapper.selectList(wrapper);
        List<MesktHistory> classList = mesktHistories.stream().filter(f -> {
            DateTime tsDate = DateUtil.parse(f.getTs());
            return DateUtil.isIn(tsDate, classStartDate, now);
        }).collect(Collectors.toList());
        List<MesktHistory> todayList = mesktHistories.stream().filter(f -> {
            DateTime tsDate = DateUtil.parse(f.getTs());
            return DateUtil.isIn(tsDate, startDate, now);
        }).collect(Collectors.toList());
        double classWd = classList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getWd())).average().orElse(0.0);
        double todayWd = todayList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getWd())).average().orElse(0.0);
        double monthWd = mesktHistories.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getWd())).average().orElse(0.0);
        double classSd = classList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getSd())).average().orElse(0.0);
        double todaySd = todayList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getSd())).average().orElse(0.0);
        double monthSd = mesktHistories.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getSd())).average().orElse(0.0);
        List<String> wdList = Arrays.asList(meskt.getWd(),String.format("%.2f",classWd),String.format("%.2f",todayWd),String.format("%.2f",monthWd));
        List<String> sdList = Arrays.asList(meskt.getSd(),String.format("%.2f",classSd),String.format("%.2f",todaySd),String.format("%.2f",monthSd));
        topMap.put("wd", wdList);
        topMap.put("sd", sdList);
        List<String> xAxisData = getHours();
        List<List<Double>> value = new ArrayList<>();
        List<Double> value1 = new ArrayList<>();
        List<Double> value2 = new ArrayList<>();
        xAxisData.forEach(hour->{
            MesktHistory kt = todayList.stream().filter(f -> {
                DateTime tsDate = DateUtil.parse(f.getTs());
                DateTime dt = DateUtil.parseTimeToday(hour);
                int tsHour = tsDate.hour(true);
                int hour1 = dt.hour(true);
                return tsHour == hour1;
            }).findFirst().orElseGet(MesktHistory::new);
            value1.add(NumberUtil.parseDouble(kt.getWd(), 0.0));
            value2.add(NumberUtil.parseDouble(kt.getSd(), 0.0));
        });
        value.add(value1);
        value.add(value2);
        chartMap.put("xAxisData", xAxisData);
        chartMap.put("value", value);
        resultMap.put("top", topMap);
        resultMap.put("chart", chartMap);
        return AjaxResult.success(resultMap);
    }

    @GetMapping("/kt/history/tableList")
    @Operation(description = "空调历史数据列表",parameters = {
            @Parameter(name = "macid",description = "编号",required = true),
            @Parameter(name = "date",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult ktHistoryTableList(String macid,@RequestParam(required = false) String date) {
        Page.startPage();
        DateTime dt = StringUtils.hasText(date) ? DateUtil.parse(date) : DateUtil.date();
        DateTime startDate = DateUtil.beginOfDay(dt);
        DateTime endOfDay = DateUtil.endOfDay(dt);
        LambdaQueryWrapper<MesktHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseEntity::getMacid, macid);
        wrapper.between(BaseEntity::getTs, startDate,endOfDay).orderByDesc(BaseEntity::getId);
        List<MesktHistory> list = mesktHistoryMapper.selectList(wrapper);
        return Page.getDataTable(list);
    }

    @GetMapping("/pds")
    @Operation(description = "配电室")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult pds() {
        LambdaQueryWrapper<MesdbReal> wrapper = new LambdaQueryWrapper<>();
        List<MesdbReal> mesdbRealList = mesdbRealMapper.selectList(wrapper);
        LambdaQueryWrapper<Bcxt06DistributionRoomElectricit> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.orderByAsc(Bcxt06DistributionRoomElectricit::getPowerDisNumber);
        List<Bcxt06DistributionRoomElectricit> electricitList = electricitMapper.selectList(wrapper1);
        List<String> pdsNames = electricitList.stream().map(Bcxt06DistributionRoomElectricit::getPowerDisRoom).distinct().collect(Collectors.toList());
        List<Object> list = pdsNames.stream().map(pdsName->{
            List<Bcxt06DistributionRoomElectricit> electricitDataList = electricitList.stream().filter(f -> f.getPowerDisRoom().equals(pdsName)).collect(Collectors.toList());
            List<MesdbReal> dbList = electricitDataList.stream().map(db -> {
                String macid = db.getFormNumber();
                String macname = db.getPowerDisNumber();
                MesdbReal mesdbReal = mesdbRealList.stream().filter(f -> f.getMacid().equals(macid) && f.getMacname().equals(macname)).findFirst().orElseGet(MesdbReal::new);
                mesdbReal.setDbname(StringUtils.hasText(mesdbReal.getDbname()) ? mesdbReal.getDbname() : db.getFormName());
                mesdbReal.setMacid(StringUtils.hasText(mesdbReal.getMacid()) ? mesdbReal.getMacid() : db.getFormNumber());
                mesdbReal.setMacname(StringUtils.hasText(mesdbReal.getMacname()) ? mesdbReal.getMacname() : db.getPowerDisNumber());
                mesdbReal.setPdsname(StringUtils.hasText(mesdbReal.getPdsname()) ? mesdbReal.getPdsname() : pdsName);
                mesdbReal.setStatus(StringUtils.hasText(mesdbReal.getStatus()) ? mesdbReal.getStatus() : "正常");
                mesdbReal.setTs(StringUtils.hasText(mesdbReal.getTs()) ? mesdbReal.getTs() : DateUtil.date().toString());
                mesdbReal.setClasses(StringUtils.hasText(mesdbReal.getClasses()) ? mesdbReal.getClasses() : "0.00");
                mesdbReal.setDay(StringUtils.hasText(mesdbReal.getDay()) ? mesdbReal.getDay() : "0.00");
                mesdbReal.setMonth(StringUtils.hasText(mesdbReal.getMonth()) ? mesdbReal.getMonth() : "0.00");
                mesdbReal.setA720(Objects.nonNull(mesdbReal.getA720()) ? mesdbReal.getA720() : 0.00);
                return mesdbReal;
            }).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            map.put("pdsName", pdsName);
            map.put("dbList", dbList);
            return map;
        }).collect(Collectors.toList());
        return AjaxResult.success(list);
    }

    @GetMapping("/pds/dbnh")
    @Operation(description = "配电室单个电表能耗数据",parameters = {
            @Parameter(name = "macid",description = "macid",required = true),
            @Parameter(name = "macname",description = "macname",required = true),
            @Parameter(name = "type",description = "1,2,3,4",required = true),

    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult pdsdbnh(@RequestParam(defaultValue = "1") int type, String macid, String macname) {
        Page.startPage();
        List<Object> list = new ArrayList<>();
        if(type == 1){//班能耗
            LambdaQueryWrapper<MesdbClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseEntity::getMacid, macid).eq(BaseEntity::getMacname, macname)
                    .orderByDesc(MesdbClass::getStartdatetime);
            list.addAll(mesdbClassMapper.selectList(wrapper));
        }else {
            //日能耗
            LambdaQueryWrapper<MesdbDay> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseEntity::getMacid, macid).eq(BaseEntity::getMacname, macname)
                    .like(type == 3, MesdbDay::getDateymd, "-01")
                    .like(type == 4, MesdbDay::getDateymd, "-21")
                    .orderByDesc(MesdbDay::getDateymd);
            list.addAll(mesdbDayMapper.selectList(wrapper));
        }
        return Page.getDataTable(list);
    }

    @GetMapping("/nhcx")
    @Operation(description = "配电室单个电表能耗数据",parameters = {
            @Parameter(name = "startDate",description = "日期"),
            @Parameter(name = "endDate",description = "日期"),
            @Parameter(name = "workshop",description = "车间"),
            @Parameter(name = "pdsName",description = "配电室"),

    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult nhcx(@RequestParam(required = false) String startDate,
                           @RequestParam(required = false) String endDate,
                           @RequestParam(required = false) String workshop,
                           @RequestParam(required = false) String pdsName) {
        DateTime start = StringUtils.hasText(startDate) ?
                DateUtil.beginOfDay(DateUtil.parse(startDate)) :
                DateUtil.parse(GetStartDate.getMonthStartDate());
        DateTime end = StringUtils.hasText(endDate) ?
                DateUtil.endOfDay(DateUtil.parse(endDate)) :
                DateUtil.date();
        LambdaQueryWrapper<Bcxt06DistributionRoomElectricit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(workshop), Bcxt06DistributionRoomElectricit::getWorkshop, workshop)
                .eq(StringUtils.hasText(pdsName), Bcxt06DistributionRoomElectricit::getPowerDisRoom, pdsName)
                .orderByAsc(Bcxt06DistributionRoomElectricit::getPowerDisNumber);
        List<Bcxt06DistributionRoomElectricit> electricitList = electricitMapper.selectList(wrapper);
        List<String> pdsNames = electricitList.stream().map(Bcxt06DistributionRoomElectricit::getPowerDisRoom).distinct().collect(Collectors.toList());

        LambdaQueryWrapper<MesdbHistory> mesdbHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mesdbHistoryLambdaQueryWrapper.between(BaseEntity::getTs, start.toString(), end.toString()).orderByAsc(BaseEntity::getTs);
        List<MesdbHistory> mesdbHistoryList = mesdbHistoryMapper.selectList(mesdbHistoryLambdaQueryWrapper);
        List<Map<String, Object>> list = pdsNames.stream().map(pds -> {
            List<Bcxt06DistributionRoomElectricit> electricitDataList = electricitList.stream().filter(f -> f.getPowerDisRoom().equals(pds)).collect(Collectors.toList());
            List<Map<String, Object>> dbList = electricitDataList.stream().map(db -> {
                String macid = db.getFormNumber();
                String macname = db.getPowerDisNumber();
                List<MesdbHistory> mesdbDataList = mesdbHistoryList.stream().filter(f -> f.getMacid().equals(macid) && f.getMacname().equals(macname)).collect(Collectors.toList());
                MesdbHistory startData = Optional.ofNullable(CollUtil.getFirst(mesdbDataList)).orElseGet(MesdbHistory::new);
                MesdbHistory endData = Optional.ofNullable(CollUtil.getLast(mesdbDataList)).orElseGet(MesdbHistory::new);
                Map<String, Object> map = new HashMap<>();
                double startNh = Optional.ofNullable(startData.getA720()).orElse(0.0);
                double endNh = Optional.ofNullable(endData.getA720()).orElse(0.0);
                double nh = endNh-startNh;
                map.put("start", startNh);
                map.put("end", endNh);
                map.put("nh", String.format("%.2f",nh));
                map.put("db", db.getFormName());
                return map;
            }).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            map.put("pdsName", pds);
            map.put("dbList", dbList);
            return map;
        }).collect(Collectors.toList());
        return AjaxResult.success(list);
    }

    @GetMapping("/tc/bzdb")
    @Operation(description = "停车分析班组对比",parameters = {
            @Parameter(name = "startDate",description = "日期"),
            @Parameter(name = "endDate",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult tcbzdb(@RequestParam(required = false) String startDate,
                           @RequestParam(required = false) String endDate) {
        DateTime start = StringUtils.hasText(startDate) ?
                DateUtil.beginOfDay(DateUtil.parse(startDate)) :
                DateUtil.parse(GetStartDate.getMonthStartDate());
        DateTime end = StringUtils.hasText(endDate) ?
                DateUtil.endOfDay(DateUtil.parse(endDate)) :
                DateUtil.date();
        long between = DateUtil.betweenDay(start, end, true);
        QueryWrapper<StopdataInfoClass> wrapper = new QueryWrapper<>();
        wrapper.ge("startdatetime", start.toString()).le("startdatetime",end.toString());
        wrapper.select("classname","sum(stopcount_other) stopcount_other","sum(stopcount_total) stopcount_total",
                "sum(stopcount_warp) stopcount_warp","sum(stopcount_weft_long) stopcount_weft_long","sum(stopcount_weft_short) stopcount_weft_short",
                "sum(stoptime_other) stoptime_other","sum(stoptime_total) stoptime_total","sum(stoptime_warp) stoptime_warp",
                "sum(stoptime_weft_long) stoptime_weft_long","sum(stoptime_weft_short) stoptime_weft_short");
        wrapper.last("group by classname");
        List<StopdataInfoClass> stopList = stopdataInfoClassService.list(wrapper);
        String jia = "甲";
        String yi = "乙";
        StopdataInfoClass jiaStop = stopList.stream().filter(f -> f.getClassname().equals(jia)).findFirst().orElseGet(StopdataInfoClass::new);
        StopdataInfoClass yiStop = stopList.stream().filter(f -> f.getClassname().equals(yi)).findFirst().orElseGet(StopdataInfoClass::new);

        List<Map<String, Object>> keyList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "短纬停次数");
        map.put("key", "stopcount_weft_short");
        map.put("divisor", 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "短纬停时长h");
        map.put("key", "stoptime_weft_short");
        map.put("divisor", 3600.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "长纬停次数");
        map.put("key", "stopcount_weft_long");
        map.put("divisor", 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "长纬停时长h");
        map.put("key", "stoptime_weft_long");
        map.put("divisor", 3600.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "经停次数");
        map.put("key", "stopcount_warp");
        map.put("divisor", 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "经停时长h");
        map.put("key", "stoptime_warp");
        map.put("divisor", 3600.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "其它停次数");
        map.put("key", "stopcount_other");
        map.put("divisor", 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "其它停时长h");
        map.put("key", "stoptime_other");
        map.put("divisor", 3600.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "总次数");
        map.put("key", "stopcount_total");
        map.put("divisor", 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "总停时长h");
        map.put("key", "stoptime_total");
        map.put("divisor", 3600.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "日均次数");
        map.put("key", "stopcount_total");
        map.put("divisor", between * 1.0);
        keyList.add(map);
        map = new HashMap<>();
        map.put("name", "日均时长h");
        map.put("key", "stoptime_total");
        map.put("divisor", 3600.0 * between);
        keyList.add(map);
        List<Map<String, Object>> list = keyList.stream().map(key -> {
            Map<String, Object> mapData = new HashMap<>();
            int jiaValue = (int) ReflectUtil.getFieldValue(jiaStop,key.get("key").toString());
            int yiValue = (int) ReflectUtil.getFieldValue(yiStop,key.get("key").toString());
            double divisor = (double) key.get("divisor");
            mapData.put("name", key.get("name"));
            mapData.put("jia", String.format("%.2f", jiaValue / divisor));
            mapData.put("yi", String.format("%.2f", yiValue / divisor));
            return mapData;
        }).collect(Collectors.toList());
        return AjaxResult.success(list);
    }

    @GetMapping("/tc/jtph")
    @Operation(description = "停车分析机台排行",parameters = {
            @Parameter(name = "date",description = "日期"),
    })
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult tcjtph(@RequestParam(required = false) String date) {
        DateTime start = StringUtils.hasText(date) ? DateUtil.parse(date) : DateUtil.date();
        LambdaQueryWrapper<StopdataInfoClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(StopdataInfoClass::getId);
        wrapper.eq(StopdataInfoClass::getDateymd, start.toDateStr());
        List<StopdataInfoClass> list = stopdataInfoClassService.list(wrapper);
        list.sort(Comparator.comparingInt(o -> NumberUtil.parseInt(o.getMacid(), 99999)));
        return AjaxResult.success(list);
    }

    private List<String> getHours(){
        List<String> hours = new ArrayList<>();
        for(int i=0;i<24;i++){
            String str = StrUtil.padPre(String.valueOf(i), 2, "0");
            hours.add(str + ":00");
        }
        return hours;
    }
}
